﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：产地管理
  //创建人：赵国伟
  //生成时间：2012/04/04 23:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class GoodsPlacesManage
    {
        private readonly DALGoodsPlaces dalPlace = new DALGoodsPlaces();

        #region "保存产地信息 by 赵国伟 2012-04-04"
        /// <summary>
        /// 保存产地信息
        /// </summary>
        /// <param name="place"></param>
        /// <returns></returns>
        public ReturnValue Save(GoodsPlaces place)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "保存失败");
            //检查产地名称是否重复；
            if (dalPlace.QueryList(1, "PlaceName='" + place.PlaceName + "' and GoodsPlaceID<>" + place.GoodsPlaceID, "") != null)
            {
                ret.Message = "产地名称重复！";
                return ret;
            }
            int result = 0;
            if (place.GoodsPlaceID <= 0)
            {
                place.CreateDate = DateTime.Now;
                result = dalPlace.Add(place);
            }
            else
            {
                GoodsPlaces o = dalPlace.GetModel(place.GoodsPlaceID);
                place.CreateDate = o.CreateDate;
                result = dalPlace.Update(place);
            }
            if (result > 0)
            {
                ret.Message = "保存成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "查询一条产地信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 查询一条产地信息；
        /// </summary>
        /// <param name="GoodsPlaceID"></param>
        /// <returns></returns>
        public GoodsPlaces GetPlaceInfo(int GoodsPlaceID)
        {
            return dalPlace.GetModel(GoodsPlaceID);
        }
        #endregion

        #region "删除指定的产地信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 删除指定的产地信息；
        /// </summary>
        /// <param name="GoodsPlaceID"></param>
        /// <returns></returns>
        public ReturnValue Delete(int GoodsPlaceID)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "删除失败！");
            try
            {
                if (dalPlace.Delete(GoodsPlaceID) > 0)
                {
                    ret.Result = ResultType.Success;
                    ret.Message = "删除成功！";
                }
            }
            catch
            {
                ret.Message = "删除失败:存在使用该产地的商品，不能删除，只能停用!";
               
            }
            return ret;
           
        }
        #endregion

        #region "返回产地信息为Hash结构； by 赵国伟 2012-04-04"
        /// <summary>
        /// 返回产地信息为Hash结构；
        /// </summary>
        public Dictionary<int, string> QueryPlacesToHash()
        {
            return BLLHelper.GenerateToHash(dalPlace.Find("", ""));
        }
        #endregion

        #region "返回产地信息并存为下拉框选项；by 赵国伟 2012-04-04"
        /// <summary>
        /// 返回产地信息并存为下拉框选项；
        /// </summary>
        /// <param name="IsAvailable"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string QueryPlacesToSelectOption(bool IsAvailable, string defaultValue)
        {
            string cond = "";
            if (IsAvailable)
                cond = "Status='" + Status.正用.ToString() + "'";
            string orderby = "OrderValue,GoodsPlaceID desc";
            DALGoodsPlaces dalPlace = new DALGoodsPlaces();
            List<IDNameObj> listOfData = dalPlace.Find(cond, orderby);
            return BLLHelper.GenerateToSelectOption(listOfData, defaultValue);
        }
        #endregion

        #region "查询产地信息； by 赵国伟 2012-04-05"
        /// <summary>
        /// 查询产地信息；
        /// </summary>
        /// <returns></returns>
        public List<GoodsPlaces> QueryPlaces()
        {
            return dalPlace.QueryList("", "OrderValue,GoodsPlaceID desc");
        }
        public List<IDNameObj> QueryPlacesEx()
        {
            return dalPlace.Find("Status='" + Status.正用.ToString() + "'", "OrderValue,GoodsPlaceID desc");
        }
        #endregion



        



    }

}
